<?php

namespace App\Models\Workflow;

use App\Models\BaseModel;
use App\Models\Shop\ShopModel;
use App\Models\Traits\KfSoftDeletes;
use App\Models\Traits\KfUid;
use App\Models\WorkOrder\WorkflowV2TypeModel;
use App\Service\BaseService;
use App\Service\WorkOrder\WorkOrderBaseService;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;

class WorkflowV2OrderModel extends BaseModel
{
    use KfUid;
    use KfSoftDeletes;

    protected $table = 'workflow_v2_orders';

    protected $fillable = [
        'workflowNo',
        'name',
        'workflowType',
        'workflowType2',
        'status',
        'submitCruiseTemplateId',
        'submitCruiseOrderId',
        'shopId',
        'relationId',
        'currentNodeId',
        'isDeleted',
        'createdByUid',
    ];

    const STATUS_ING    = 0;
    const STATUS_FINISH = 1;
    const STATUS_CLOSE  = 2;

    const STATUS_TEXT = [
        self::STATUS_FINISH => '已完成',
        self::STATUS_ING    => '进行中',
        self::STATUS_CLOSE  => '已关闭',
    ];

    const CAN_APPROVAL_N = 0;
    const CAN_APPROVAL_Y = 1;

    const CAN_APPROVAL_MAPPING = [
        self::CAN_APPROVAL_N=>'不可审核',
        self::CAN_APPROVAL_Y=>'可审核',
    ];

    public function nodesRel(): HasMany
    {
        return $this->hasMany(WorkflowV2OrderNodeModel::class, 'workflowOrderId', 'id');
    }

//    public function logsRel(): HasMany
//    {
//        return $this->hasMany(WorkflowOrderLogModel::class, 'workflowOrderId', 'id');
//    }

//    /**
//     * 获取当前工单的自定义节点状态
//     * @return string
//     */
//    public function nodeDefineStatusText(): string
//    {
//        $currentMainNode = $this->nodesRel->where('status', WorkflowOrderNodeModel::STATUS_ING)->first();
//        if (!$currentMainNode) {
//            return '已完成';
//        }
//
//        $text = '';
//
//        switch ($currentMainNode->businessType) {
//            case  WorkflowEnum::BUSINESS_TYPE_SUBMIT_APPLY:
//                $text = '待提交';
//                break;
//            case WorkflowEnum::BUSINESS_TYPE_FIX_AUDIT:
//                if ($currentMainNode->handlerType == WorkflowEnum::HANDLER_TYPE_HEAD) {
//                    $text = '待复审';
//                } elseif ($currentMainNode->handlerType == WorkflowEnum::HANDLER_TYPE_PROVINCE) {
//                    $text = '待初审';
//                } elseif ($currentMainNode->handlerType == WorkflowEnum::HANDLER_TYPE_SUPERVISE) {
//                    $text = '待初审';
//                }
//
//                break;
//            case WorkflowEnum::BUSINESS_TYPE_RENEW_SIGN:
//                $text = '待签署';
//                break;
//        }
//
//        return $text;
//    }

    // 生成工单号
    public function generateWorkflowNo(): string
    {
        $workflowV2Type = WorkflowV2TypeModel::query()
                                             ->where('id', $this->workflowType2)
                                             ->where('type', $this->workflowType)
                                             ->first();

        $prefix = optional($workflowV2Type)->prefix ?: 'DF'; // default prefix

        $dateTime = date('YmdHis');
        $count    = str_pad(WorkOrderBaseService::autoincrementByRedis($prefix . $this->id), 4, "0", STR_PAD_LEFT);
        return $prefix . $this->id . $dateTime . $count;
    }




    public function workFlowV2TypeRel():HasOne
    {
        return $this->hasOne(WorkflowV2TypeModel::class,'id','workflowType2');
    }

    public function shopRel():HasOne
    {
        return $this->hasOne(ShopModel::class,'id','shopId');
    }

    public function canApproval():int
    {
        //todo 判断当前人、单子状态
        return self::CAN_APPROVAL_Y;//可审核
    }

    public function currentNodeRel():HasOne
    {
        return $this->hasOne(WorkflowV2OrderNodeModel::class,'id','currentNodeId');
    }

    public function currentNodeHandler():HasMany
    {
        return $this->hasMany(WorkflowV2OrderNodeHandlerModel::class,
                'workflowOrderNodeId','currentNodeId');
    }
}
